package team.ttorder.ttorder.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import team.ttorder.ttorder.model.Food;
import team.ttorder.ttorder.model.Table;
import team.ttorder.ttorder.service.IFoodService;
import team.ttorder.ttorder.service.ITableService;
import team.ttorder.ttorder.utils.CommonMessageResult;

@RestController
@RequestMapping("/table")
public class TableController {

	@Autowired
	private ITableService tableService;
	
	@Autowired
	private IFoodService footService;

	/*
	 * @RequestMapping(value = "/manageTables", method = RequestMethod.GET)
	 * public ModelAndView manageTable(ModelMap modelMap) { return new
	 * ModelAndView("business/table/manageTable"); }
	 * 
	 * @RequestMapping(value = "/manageTable", method = RequestMethod.POST)
	 * public PageBean<Table> table(ModelMap modelMap, Integer pageNum, Integer
	 * pageSize) { PageBean<Table> pageBean = tableService.selectAll(pageNum,
	 * pageSize); return pageBean; }
	 */

	@RequestMapping(value = "/manageTables")
	private ModelAndView toManageTable(Model model) {
		List<Table> tableList = tableService.findAllTables();
		model.addAttribute("tableList", tableList);
		return new ModelAndView("business/table/manageTable");
	}

	@ResponseBody
	@RequestMapping(value = "/addTable", method = RequestMethod.POST)
	public CommonMessageResult addTable(@Valid Table table, BindingResult bindingResult) throws Throwable {
		CommonMessageResult result = new CommonMessageResult();
		Map<String,String> messageMap=new HashMap<String,String>();
		result.setSuccess(false);
		
		try {
			tableService.addTable(table);
			result.setSuccess(true);
		} catch (Exception e) {
			
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			result.setMessageMap(messageMap);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/deleteTable", method = RequestMethod.POST)
	public CommonMessageResult deleteTable(int id) {
		CommonMessageResult result = new CommonMessageResult();
		Map<String, String> messageMap = new HashMap<String, String>();
		result.setSuccess(false);
		try {
			tableService.deleteTable(id);
			result.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			result.setMessageMap(messageMap);
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getTableStatus", method = RequestMethod.POST)
	public List<Table> getTableStatus(int tablesStatus) {
		List<Table> result = tableService.selectTablesStatus(tablesStatus);
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getTableId", method = RequestMethod.POST)
	public Table getTableId(int id) {
		Table result = tableService.selectTableById(id);

		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/uptadeTable", method = RequestMethod.POST)
	public CommonMessageResult updateTable(@Valid Table table, BindingResult bindingResult, int id) {
		CommonMessageResult result = new CommonMessageResult();
		Map<String, String> messageMap = new HashMap<String, String>();
		result.setSuccess(false);
		
		try {

			tableService.updaTetable(table);
			result.setSuccess(true);
			result.setMessageMap(messageMap);

		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			result.setMessageMap(messageMap);
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/food", method = RequestMethod.POST)
	public List<Food> getFood(int foodTypeId) {
		List<Food> result = tableService.selectFood(foodTypeId);
		return result;
	}
	
	@RequestMapping(value = "/foodName", method = RequestMethod.POST)
	public Food getFoodName(int id) {
		Food result = footService.getFoodById(id);
		return result;
	}
}
